package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/19 19:42
 */
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;


    @Override
    public User getByusername(String username) {
        //通过username获得user对象
        User user = userDao.getByUsername(username);
        Integer id = user.getId();
        //通过userid查询角色
        Set<Role> roleList = roleDao.getByuserId(id);
        //循环rolelist查询角色具有的功能
        for (Role role : roleList) {
            Integer roleId = role.getId();
            //通过角色id查询功能
            Set<Permission> permissionList = permissionDao.getByroleId(roleId);
            role.setPermissions(permissionList);
        }
        user.setRoles(roleList);
        return user;
    }

    @Override
    @Transactional
    public void add(User user,Integer[] roleIds) {
        //向检查组中添加数据
        userDao.add(user);
        Integer id = user.getId();
        //向中间表中添加数据
        for (Integer roleId : roleIds) {
            userDao.addByUserIdandRoleId(id,roleId);
        }
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        //删除中间表数据
        userDao.deleteById(id);
        //删除基本表数据
        userDao.delete(id);
    }

    @Override
    @Transactional
    public void update(User user,Integer[] roleIds) {
        //更新checkgroup的基本数据
        userDao.update(user);
        //根据id删除全部的中间表的信息
        userDao.deleteById(user.getId());
        //向中间表中添加关系
        for (Integer roleId : roleIds) {
            userDao.addByUserIdandRoleId(user.getId(),roleId);
        }
    }

    @Override
    public User get(Integer id) {
        return userDao.get(id);
    }


    @Override
    public PageResult getByConditions(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<User> users = userDao.getByConditions(queryPageBean.getQueryString());
        PageInfo info = new PageInfo(users);
        PageResult pageResult = new PageResult(info.getTotal(),info.getList());
        return pageResult;
    }

    @Override
    public List<Integer> getRoleIdsById(Integer id) {
        return userDao.getRoleIdsById(id);
    }

    @Override
    public List<Role> getAll() {
        return roleDao.getAll();
    }
}
